package asteroids.model.expressions;

import java.util.HashMap;
import java.util.Map;

import asteroids.model.*;
import asteroids.model.types.T;

/**
 * A subclass of expression (E) that represents all the unary expressions
 * 
 * A unary expression is an expression that has one expression as argument
 * 
 * @version 1.0
 * 
 * @author	Thijs Peirelinck & Wouter Parys
 *
 */
public abstract class UnaryExpression extends ComposedExpression {
	
	private E e;
	
	public UnaryExpression(E e) {
		this.e = e;
	}
	
	@Override
	public abstract boolean isValidExpression();
	
	@Override
	public abstract Object getValue();
	
	public E getOperand() {
		return e;
	}
	
	@Override
	public abstract String getReturnType();
	
	@Override
	public abstract String toString();

	public void setOperand(E expr) {
		this.e = expr;
	}
	
	@Override
	public void validate(HashMap<String, E> variables) {
		if(this.getOperand() instanceof Variable) {
			setOperand(super.getValidatedOperand(variables, this.getOperand()));	
		}
		else if(this.getOperand() instanceof ComposedExpression) {
			((ComposedExpression)this.getOperand()).validate(variables);
		}
	}
	
	@Override
	public void setExecuter(Ship executer) {
		getOperand().setExecuter(executer);
	}
	
	public boolean typeCheck(Map<String, T> globals, String type) {
		if(getOperand() instanceof Variable) {
			if(globals.get(getOperand().getValue()) == null)
				return false;
			return globals.get(getOperand().getValue()).getReturnType().equals(type);
		}
		return getOperand().typeCheck(globals);
	}
}
